import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';

import { Fun } from '../providers/fun';
import { TabsPage } from '../pages/tabs/tabs';
import { LoginPage } from '../pages/login/login';
import { LogPage } from '../pages/Log/log';

import { DocumentsPage } from '../pages/documents/documents';
import { WeChatApi } from '../providers/wechatapi';



@Component({
  templateUrl: 'app.html'
})
export class Governor {

  rootPage: any;


  constructor(platform: Platform, public wechatapi: WeChatApi) {
  
    if (this.GetQueryString("code")) {
      (<any>window).code = this.GetQueryString("code");
      this.wechatapi.GetWChatToken((<any>window).code).subscribe((res) => {
        (<any>window).openid = res.data.token;

        alert((<any>window).openid);

        // that.wechatapi.getscore((<any>window).openid).subscribe((res) => {
        //     if (res && res.data && res.data[0]) {

        //         var openid = res.data[0].openid;
        //         if (openid == (<any>window).openid) {
        //             (<any>window).tag = 1;
        //               that.appCtrl.getRootNav().setRoot(TabsPage);
        //         }
        //         else {


        //         }
        //     }
        // });
      })
    }

    if (Fun.isLogin()) {
      this.rootPage = TabsPage;
    }
    else {
      this.rootPage = LoginPage;
    }
    platform.ready().then(() => {
      StatusBar.styleDefault();
      Splashscreen.hide();
    });
  }
  GetQueryString(name) {
    //   static num=0;
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return r[2]; return null;
  }
}
